import axios from 'axios'
import store from '@/store'
import { Message } from 'element-ui'
const sercive =axios.create({
    baseURL:process.env.VUE_APP_BASE_API,//根据启动环境变量获取不同的请求地址
    timeout:10000
})
//请求拦截器
sercive.interceptors.request.use(config=>{
//注入token
//判断有无请求头做出相应处理
    if(store.getters.token){
        config.headers.Authorization=`Bearer ${store.getters.token}`
    }
    return config
},(error)=>{
    return Promise.reject(error)
})
//响应拦截器
sercive.interceptors.response.use(response=>{
    //解构response
    const{data,message,success}=response.data
    if(success){
        return data
    }else{
        Message({type:'error',message})
        return Promise.reject(new Error(message))
    }
}
,(error)=>{
    Message({type:'error',message:error.message||'请求失败'})
    return Promise.reject(error)
}
)
export default sercive